package cn.itcast.server.handler;

import cn.itcast.message.ChatRequestMessage;
import cn.itcast.message.ChatResponseMessage;
import cn.itcast.server.session.SessionFactory;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

@ChannelHandler.Sharable
public class ChatRequestMessageHandler extends SimpleChannelInboundHandler<ChatRequestMessage>{

	@Override
	protected void channelRead0(ChannelHandlerContext ctx, ChatRequestMessage msg) throws Exception {
		String to = msg.getTo(); //收消息者
		Channel channel = SessionFactory.getSession().getChannel(to);
		if(channel != null) {
			// 在线
			channel.writeAndFlush(new ChatResponseMessage(msg.getFrom(), msg.getContent()));
		}else {
			// 不在线
			ctx.writeAndFlush(new ChatResponseMessage(false, "对方用户不存在或者不在线"));
		}
	}

}
